package com.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.util.BaseAction;
import com.util.JsonUtil;
import com.util.MD5Util;
import com.web.domain.User;
import com.web.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

//因为Struts2在每次接受到请求后，会迅速的创建actionContext,ValueStack,action   
//然后把Action存在在ValueStack中 所以Action可以被OGNL访问.  
//但是把Action交给Spring管理后,action 就是单利模式了,这样 就违背了Struts2的设计理念。  
//所以 我们需要 将action 声明为 原型 @Scope("prototype")  

@Controller
@Scope("prototype")
@ParentPackage(value = "struts-default")
@Namespace(value = "/")
@Action(value = "userAction", results = {
        @Result(name = "input", type = "redirect", location = "/login/login.jsp"),
        @Result(name = "userInfo", location = "/user_info/user_info.jsp"),
        @Result(name = "update", location = "/main/main.jsp")
})
public class UsertAction extends BaseAction {
    @Autowired
    private UserService userService;
    private User user;
    private String state;

    public String execute() throws Exception {
        return null;
    }

    public String addUI() throws Exception {
        return "add";
    }

    public String add() throws Exception {
        userService.save(user);
        ActionContext.getContext().put("msg", "保存成功！");
        return "message";
    }

    //退出登录
    public String logout() throws Exception {
        session.removeAttribute("user");
        session.invalidate();
        return INPUT;
    }

    //登录校验
    public String login() throws Exception {
        // 获得图形校验码
        Map<String, String> map = new HashMap<String, String>();
        String username = "";
        User userInfo = userService.find(user.getUserName());
        String PASSWORD = MD5Util.getEncryptPsw(user.getPassword());
        if (userInfo != null && userInfo.getPassword().equals(PASSWORD)) {
            session.setAttribute("user", user);
            setLoginTime(user);
            username = user.getUserName();
        }
        map.put("username", username);
        JsonUtil.jsonOut(map);
        return NONE;
    }

    private void setLoginTime(User user) {
        user.setLoginTime(new java.util.Date());
        userService.saveOrUpdateUser(user);
    }

    public String showUserInfoView() {
        return "userInfo";
    }


    public String saveOrUpdateUser() {
        userService.saveOrUpdateUser(user);
        if (StringUtils.equals(state, "add")) {
            return "input";
        } else {
            session.setAttribute("user", user);
            return null;
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}
